﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL_MechSys;
using DTO_MechSys;

namespace FORMS_MechSys
{
    public partial class frmClienteSelecionar : Form
    {
        BLLCliente bllCliente = new BLLCliente();
        List<DTOCliente> ListClientes;

        DTOCor dtoCorPrincipal = new DTOCor();
        DTOCor dtoCorFundo = new DTOCor();
        DTOCor dtoCorLetra = new DTOCor();
        DTOCor dtoCorDataGrid = new DTOCor();

        BLLCor bllCor = new BLLCor();



        public frmClienteSelecionar()
        {
            InitializeComponent();
            SetColors();
            PopulaGrid();
        }

        private void SetColors()
        {
            dtoCorPrincipal = bllCor.Buscar("Principal");
            tlpMenuStripLeft.BackColor = tlpMenuStripRigth.BackColor = toolStripButtonSair.BackColor =
            tlpMenuStripCenter.BackColor = tableLayoutPanel5.BackColor = label4.BackColor =
            btnNovoCliente.BackColor = btnPesquisar.BackColor = dtoCorPrincipal.Cor;

            dtoCorFundo = bllCor.Buscar("Fundo");
            tlpMenuStrip.BackColor =
            tableLayoutPanel1.BackColor = dtoCorFundo.Cor;

            dtoCorDataGrid = bllCor.Buscar("DataGrid");
            dtGridSelecionaCliente.BackgroundColor = dtoCorDataGrid.Cor;

            dtoCorLetra = bllCor.Buscar("Letra");
            btnNovoCliente.ForeColor =toolStripButtonSair.ForeColor = label4.ForeColor = dtoCorLetra.Cor;


        }

        public void PopulaGrid()
        {
            DataRow dataRow;
            DataTable dataTableGridView = new DataTable();

            ListClientes = bllCliente.BuscarClientesCadastrados();

            try
            {
                if (ListClientes.Count > 0)
                {
                    dataTableGridView.Columns.Add("ClienteID", typeof(int));
                    dataTableGridView.Columns.Add("Nome", typeof(string));
                    dataTableGridView.Columns.Add("Sobrenome", typeof(string));
                    dataTableGridView.Columns.Add("CPF", typeof(string));
                    dataTableGridView.Columns.Add("Nascimento", typeof(string));
                    dataTableGridView.Columns.Add("Telefone", typeof(string));
                    dataTableGridView.Columns.Add("Celular", typeof(string));
                    dataTableGridView.Columns.Add("Email", typeof(string));
                    dataTableGridView.Columns.Add("EnderecoID", typeof(int));


                    foreach (DTOCliente dtoCliente in ListClientes)
                    {
                        dataRow = dataTableGridView.NewRow();

                        dataRow["ClienteID"] = dtoCliente.ClienteID;
                        dataRow["Nome"] = dtoCliente.Nome;
                        dataRow["Sobrenome"] = dtoCliente.Sobrenome;
                        dataRow["CPF"] = dtoCliente.CPF;
                        dataRow["Nascimento"] = string.Format("{0:d}", dtoCliente.DataNascimento);
                        dataRow["Telefone"] = dtoCliente.Telefone;
                        dataRow["Celular"] = dtoCliente.Celular;
                        dataRow["Email"] = dtoCliente.Email;
                        dataRow["EnderecoID"] = dtoCliente.EnderecoID;

                        // Adiciona linha ao DataTable.
                        dataTableGridView.Rows.Add(dataRow);

                    }
                }

                dtGridSelecionaCliente.DataSource = dataTableGridView;
            }
            catch(Exception)
            {
                if (new frmMessageBox().Show("Não foi possível carregar os dados para o grid!", "Carregamento", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
                {
                    PopulaGrid();
                }
            }           
        }

        public void PopulaGrid(string texto)
        {
            DataRow dataRow;
            DataTable dataTableGridView = new DataTable();

            ListClientes = bllCliente.PesquisarClientes(texto);

            try
            {
                if (ListClientes.Count > 0)
                {
                    dataTableGridView.Columns.Add("ClienteID", typeof(int));
                    dataTableGridView.Columns.Add("Nome", typeof(string));
                    dataTableGridView.Columns.Add("Sobrenome", typeof(string));
                    dataTableGridView.Columns.Add("CPF", typeof(string));
                    dataTableGridView.Columns.Add("Nascimento", typeof(string));
                    dataTableGridView.Columns.Add("Telefone", typeof(string));
                    dataTableGridView.Columns.Add("Celular", typeof(string));
                    dataTableGridView.Columns.Add("Email", typeof(string));
                    dataTableGridView.Columns.Add("EnderecoID", typeof(int));


                    foreach (DTOCliente dtoCliente in ListClientes)
                    {
                        dataRow = dataTableGridView.NewRow();

                        dataRow["ClienteID"] = dtoCliente.ClienteID;
                        dataRow["Nome"] = dtoCliente.Nome;
                        dataRow["Sobrenome"] = dtoCliente.Sobrenome;
                        dataRow["CPF"] = dtoCliente.CPF;
                        dataRow["Nascimento"] = string.Format("{0:d}", dtoCliente.DataNascimento);
                        dataRow["Telefone"] = dtoCliente.Telefone;
                        dataRow["Celular"] = dtoCliente.Celular;
                        dataRow["Email"] = dtoCliente.Email;
                        dataRow["EnderecoID"] = dtoCliente.EnderecoID;

                        // Adiciona linha ao DataTable.
                        dataTableGridView.Rows.Add(dataRow);

                    }
                }

                dtGridSelecionaCliente.DataSource = dataTableGridView;
            }
            catch (Exception)
            {
                new frmMessageBox().Show("Registro não encontrado!", "Pesquisa", MessageBoxButtons.OK, MessageBoxIcon.Information);               
            }
           
        }

        private void dtGridSelecionaCliente_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                string nome = (string)dtGridSelecionaCliente.Rows[e.RowIndex].Cells["Nome"].Value;
                string sobrenome = (string)dtGridSelecionaCliente.Rows[e.RowIndex].Cells["Sobrenome"].Value;
                BLLUtilitarios.clienteNome = string.Format("{0} {1}", nome, sobrenome);
                BLLUtilitarios.clienteID = (int)dtGridSelecionaCliente.Rows[e.RowIndex].Cells["ClienteID"].Value;
                this.Close();
            }
        } 

        private void frmAgendamentoSelCliente_Load(object sender, EventArgs e)
        {

        }

        private void toolStripButtonSair_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPesquisar_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPesquisar.Text))
            {
                PopulaGrid(txtPesquisar.Text);
            }
        }

        private void btnNovoCliente_Click(object sender, EventArgs e)
        {
            frmClienteCadastro frmclienteCadastro = new frmClienteCadastro();
            frmclienteCadastro.ShowDialog();
        }
    }
}
